package com.qcby.financeservice.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qcby.finance.base.entity.po.SpuPo;
import com.qcby.finance.base.mapper.SpuMapper;
import com.qcby.finance.base.service.SpuService;
import com.qcby.finance.client.dto.SpuDTO;
import com.qcby.finance.client.service.SpuDubboService;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 劝书
 * @since 2023-02-19
 */
@DubboService
public class SpuDubboServiceImpl implements SpuDubboService {

    @Resource
    private SpuMapper spuMapper;
    /**
     * 上架下架
     *
     * @param spuDTO spu dto
     * @return {@link Boolean}
     */
    @Override
    public Boolean isOnSale(SpuDTO spuDTO) {
        SpuPo spuPo = BeanUtil.copyProperties(spuDTO,SpuPo.class);
        LambdaUpdateWrapper<SpuPo> updateWrapper = new LambdaUpdateWrapper<SpuPo>()
                .eq(SpuPo::getSpuName,spuPo.getSpuName())
                .set(SpuPo::getOnSale,spuPo.getOnSale());
        return spuMapper.update(null,updateWrapper)>0;
    }

    /**
     * 增加库存数量
     *
     * @param spuNo spu编号
     * @param count 数量
     * @return {@link Boolean}
     */
    @Override
    public Boolean addCount(String spuNo,Integer count) {
        LambdaQueryWrapper<SpuPo> spuPO = new LambdaQueryWrapper<SpuPo>().eq(SpuPo::getSpuNo, spuNo);
        SpuPo spuPO1 = spuMapper.selectOne(spuPO);
        spuPO1.setCount(spuPO1.getCount()+count);
        int i = spuMapper.updateById(spuPO1);
        return i > 0;
    }

    /**
     * 减少库存数量
     *
     * @param spuNo spu编号
     * @param count 数量
     * @return {@link Boolean}
     */
    @Override
    public Boolean reduceCount(String spuNo,Integer count) {
        LambdaQueryWrapper<SpuPo> spuPO = new LambdaQueryWrapper<SpuPo>().eq(SpuPo::getSpuNo, spuNo);
        SpuPo spuPO1 = spuMapper.selectOne(spuPO);
        spuPO1.setCount(spuPO1.getCount()-count);
        int i = spuMapper.updateById(spuPO1);
        return i > 0;
    }

    /**
     * 选择列表
     *
     * @param list 列表
     * @return {@link List}<{@link SpuDTO}>
     */
    @Override
    public List<SpuDTO> selectList(List<String> list) {
        LambdaQueryWrapper<SpuPo> lqwSpu= new LambdaQueryWrapper<SpuPo>().in(SpuPo::getSpuNo,list);
        List<SpuPo> spuPos = spuMapper.selectList(lqwSpu);
        List<SpuDTO> spuDTOS = BeanUtil.copyToList(spuPos, SpuDTO.class);
        return spuDTOS;
    }

    /**
     * 选择一个
     *
     * @param spuNo spu没有
     * @return {@link SpuDTO}
     */
    @Override
    public SpuDTO selectOne(String spuNo) {
        LambdaQueryWrapper<SpuPo> eq = new LambdaQueryWrapper<SpuPo>().eq(SpuPo::getSpuNo, spuNo);
        SpuPo spuPo = spuMapper.selectOne(eq);
        SpuDTO spuDTO = BeanUtil.copyProperties(spuPo, SpuDTO.class);
        return spuDTO;
    }
}
